<?php
if (!isset($_SESSION)) session_start();

$navbar_file = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/navbar.json";
$pending_navbar = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/pending_navbar.json";

if (! function_exists('objectToArray'))  {
	function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}
}

	
$string=<<<EOS
{"school_leavers":[
{"text":"Why choose PRC?",
"url":"why.html"},
{"text":"Helping you choose the right course",
"url":"right_course.html"},
{"text":"Guide to qualifications",
"url":"guide.html"},
{"text":"Financial support",
"url":"financial.html"},
{"text":"How to apply",
"url":"apply.html"},
{"text":"Transport",
"url":"transport.html"}],
"apprenticeships":[
{"text":"Apprentices","url":"apprentices.html",
"subnav" : [
	{"text":"The basics","url":"apprentice_basics.html"},
	{"text":"The benefits","url":"apprentice_benefits.html"},
	{"text":"How to become an Apprentice","url":"become_an_apprentice.html"},
	{"text":"Top tips to apply","url":"apprentice_top_tips.html"}
	]
},
{"text":"Employers","url":"employers.html",
"subnav" : [
	{"text":"The basics","url":"employer_basics.html"},
	{"text":"The business benefits","url":"employer_benefits.html"},
	{"text":"What our employers say","url":"employer_say.html"},
	{"text":"Getting started","url":"employer_get_started.html"}
]
},
{"text":"FAQ",
"url":"faqs.html"},
{"text":"Contact us",
"url":"contact.html"}],
"UCP":[
	{"text":"Why chose UCP?","url":"http://{$_SERVER['SERVER_NAME']}/UCP/overview.html"},
	{"text":"Courses","url":"http://{$_SERVER['SERVER_NAME']}/UCP/right_course.html"},
	{"text":"How to apply","url":"http://{$_SERVER['SERVER_NAME']}/UCP/apply.html"},
	{"text":"Fees and finances","url":"http://{$_SERVER['SERVER_NAME']}/UCP/fees.html"},
	{"text":"Student housing","url":"http://{$_SERVER['SERVER_NAME']}/UCP/housing.html"},
	{"text":"News and events","url":"http://{$_SERVER['SERVER_NAME']}/UCP/events.html"},
	{"text":"Student support","url":"http://{$_SERVER['SERVER_NAME']}/UCP/supprt.html"},
	{"text":"Student Union","url":"http://{$_SERVER['SERVER_NAME']}/UCP/union.html"},
	{"text":"Why choose UCP?","url":"http://{$_SERVER['SERVER_NAME']}/UCP/why.html"}
	],
"adult_learning":[
{"text":"Qualification levels",
"url":"levels.html"},
{"text":"Help with finances",
"url":"finances.html"},
{"text":"How to apply & enrol",
"url":"apply.html"},
{"text":"Terms & conditions",
"url":"terms.html"}],
"business_employer":[{"text":"Benefits to business",
"url":"overview.html"},
{"text":"Business Needs Analysis",
"url":"needss.html"},
{"text":"Training \/ Course we offer",
"url":"training.html"},
{"text":"Redundancy Support",
"url":"redundancy.html"},
{"text":"Testimonials",
"url":"testimonials.html"},
{"text":"Our partners",
"url":"partners.html"},
{"text":"Employer charter",
"url":"charter.html"},
{"text":"FAQ",
"url":"faqs.html"},
{"text":"Contact us",
"url":"contact.html"}],
"international":[{"text":"About Peterborough",
"url":"overview.html"},
{"text":"About the Park Crescent Campus",
"url":"about.html"},
{"text":"Courses",
"url":"courses.html"},
{"text":"Fees",
"url":"fees.html"},
{"text":"How to apply",
"url":"apply.html"},
{"text":"Visa information",
"url":"visa.html"},
{"text":"Accommodation",
"url":"accommodation.html"},
{"text":"International student support",
"url":"support.html"},
{"text":"Frequently asked questions",
"url":"faqs.html"},
{"text":"Useful websites",
"url":"websites.html"}],
"locations":[{"text":"Main campus",
"url":"main_campus.html"},
{"text":"City learning centre",
"url":"eating_places.html"},
{"text":"Media & Journalisim centre",
"url":"media_journalism.html"}],
"Statements & policies":[{"text":"Mission and Vision",
"url":"mission_vision.html"},
{"text":"Equality and diversity",
"url":"equality_diversity.html"},
{"text":"Data protection",
"url":"data_protection.html"},
{"text":"Freedom of information",
"url":"Freedom_information.html"},
{"text":"College charter",
"url":"college_charter.html"}],
"Leadership":[{"text":"Leadership group",
"url":"leadership_group.html"},
{"text":"Corporation board",
"url":"corporation_board.html"},
{"text":"Committees",
"url":"comittees.html"},
{"text":"Governance",
"url":"governance.html"}],
"Locations":[{"text":"Park Crescent main campus",
"url":"park_crescent.html"},
{"text":"City learning centre",
"url":"city_learning_centre.html"},
{"text":"Media & journalism centre",
"url":"comittees.html"}],
"general_facilities":[{"text":"College shops",
"url":"college_shops.html"},
{"text":"Eating places",
"url":"eating_places.html"},
{"text":"Prayer & contemplation",
"url":"prayer_contemplation.html"},
{"text":"Common room",
"url":"common_room.html"},
{"text":"Library+","url":"library.html"},
{"text":"College Sports facilities","url":"sports_facilities.html"}],
"learning_support":[{"text":"Autism or Asperger Syndrome",
"url":"autism_asperger.html"},
{"text":"Dyslexia",
"url":"dyslexia.html"},
{"text":"Mental health",
"url":"mental_health.html"},
{"text":"D\/deaf \/ Hard of hearing",
"url":"deaf.html"},
{"text":"Physical disabilities",
"url":"physical_disabilities.html"},
{"text":"Learning difficulties",
"url":"learning_difficulties.html"},
{"text":"Blind or partially sighted",
"url":"blind.html"},
{"text":"Getting support \/contact us",
"url":"contact.html"},
{"text":"Useful links",
"url":"links.html"}],
"news_events":[
{"text":"News","url":"news.html"},
{"text":"Events","url":"events.html"}],
"leadership":[{"text":"College Leadership Group",
"section":"leadership",
"url":"leadership.html"},
{"text":"Corporation Board",
"url":"corporation.html"},
{"text":"Committees",
"url":"committees.html"}],
"college_nursery":[
{"text":"Opening times & fees","section":"college_nursery","url":"Opening.html"},
{"text":"Ofsted ","section":"nursery_ofsted","url":"nursery_ofsted.html"},
{"text":"Contact Us","section":"nursery_contact","url":"nursery_contact.html"}
],
"governance":[
{"text":"Corporate Governance","url":"corporate_governance.html"},
{"text":"Governor Profiles","url":"governor_profiles.html"},
{"text":"Corporation Board Meetings","url":"corporation-board_meetings.html"}],
"rugby_centre":[{"text":"Club Information",
"section":"rugby_centre",
"url":"club_info.html"},
{"text":"News",
"url":"news.html"},
{"text":"Contact",
"url":"contact.html"},
{"text":"Photos",
"url":"photos_football.html"},
{"text":"Photos",
"url":"photos.html"},
{"text":"Photos",
"url":"photos.html"},
{"text":"Photos",
"url":"photos.html"},
{"text":"Contact",
"url":"contact.html"}],
"recruitment_process":[{"text":"The Interview",
"section":"recruitment_process",
"url":"interview.html"},
{"text":"Equal Opportunities Monitoring",
"url":"equality.html"}],
"revive":[{"text":"Hairdressing ",
"section":"revive",
"url":"hair.html"},
{"text":"Complementary Therapies ",
"url":"comp.html"},
{"text":"Contact Revive",
"url":"contact.html"}],
"hire_facilities":[{"text":"Room Hire Costs",
"section":"hire_facilities",
"url":"room_costs.html"},
{"text":"College Main Hall",
"url":"main_hall.html"},
{"text":"Parcs Conference Room",
"url":"parcs.html"},
{"text":"Food Court & Kitchen",
"url":"food.html"},
{"text":"Board Room",
"url":"board_room.html"},
{"text":"Computer Suites & Conference Rooms",
"url":"suites.html"},
{"text":"TV Studio",
"url":"tv.html"}],
"football_centre":[{"text":"Club Information",
"section":"football_centre",
"url":"club_info.html"},
{"text":"News",
"url":"news.html"}],
"statements_policies":[
{"text":"Equality & Diversity","section":"statements_policies","url":"equality.html"}],
"about":[
{"text":"Welcome", "section": "welcome","url":"http://{$_SERVER['SERVER_NAME']}/about/welcome.html"},
{"text":"Why PRC","section":"why_prc","url":"http://{$_SERVER['SERVER_NAME']}/about/why_prc.html"},
{"text":"Term dates","section":"term_dates","url":"http://{$_SERVER['SERVER_NAME']}/about/term_dates.html"},
{"text":"Locations","section":"locations","url":"http://{$_SERVER['SERVER_NAME']}/about/our_locations.html"}],
"parcs": [
{"text":"Opening Times","section":"parcs_opening_times","url":"parcs_opening_times.html"},
{"text":"Sample Menus","section":"parcs_sample_menus","url": "parcs_sample_menus.html"},
{"text":"Picture Gallery","section":"parcs_picture_gallery","url":"parcs_picture_gallery.html"}
]
}
EOS;

if (isset($_REQUEST["save"])) {
    if (is_writable($navbar_file)) {
	$navbar_1 =json_decode($string);
	$navbar = objectToArray($navbar_1);
	$fp = fopen($navbar_file, 'w');
	fwrite($fp, json_encode($navbar));
	fclose($fp);
    } else {
        echo "<pre>";
        echo "$navbar_file is not writable";
        print_r(stat($navbar_file));
        echo "</pre>";
    }
}

if ((isset($_SESSION["username"]))&&(file_exists($pending_navbar))) $navbar = objectToArray(json_decode(file_get_contents($pending_navbar)));
else $navbar = objectToArray(json_decode(file_get_contents($navbar_file)));

if (isset($_REQUEST["text"])) echo json_encode($navbar);
?>